package _mst;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 面试题 03.06. 动物收容所
 */
public class T0306 {
    private final Queue<Animal> catQueue;
    private final Queue<Animal> dogQueue;
    int age = 0;

    public T0306() {
        catQueue = new LinkedList<>();
        dogQueue = new LinkedList<>();
    }

    public void enqueue(int[] animal) {
        switch (animal[1]) {
            case 0 -> catQueue.offer(new Animal(animal[0], age));
            case 1 -> dogQueue.offer(new Animal(animal[0], age));
        }
        age++;
    }

    public int[] dequeueAny() {
        if (dogQueue.isEmpty() && catQueue.isEmpty()) return new int[]{-1, -1};
        if (catQueue.isEmpty() || (!dogQueue.isEmpty() && dogQueue.peek().age < catQueue.peek().age))
            return dequeueDog();
        else return dequeueCat();
    }

    public int[] dequeueDog() {
        if (dogQueue.isEmpty()) return new int[]{-1, -1};
        else return new int[]{dogQueue.poll().no, 1};
    }

    public int[] dequeueCat() {
        if (catQueue.isEmpty()) return new int[]{-1, -1};
        else return new int[]{catQueue.poll().no, 0};
    }

    private static class Animal {
        private int no;
        private int age;

        public Animal(int no, int age) {
            this.no = no;
            this.age = age;
        }

        public int getNo() {
            return no;
        }

        public int getAge() {
            return age;
        }
    }
}
